package com.xs.gis.cover.coverImpl;

import com.xs.gis.cover.SimpleCover;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.geotools.geometry.jts.WKBReader;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBWriter;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * @description:
 * @author: w
 * @create: 2023-08-17 17:43
 **/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class WkbCover implements SimpleCover {


    private String  charsetName;


    @Override
    public Geometry dataToGeometry(String data) throws ParseException {
        byte[] bytes = WKBReader.hexToBytes(data);
        org.locationtech.jts.io.WKBReader reader = new org.locationtech.jts.io.WKBReader();
        return reader.read(bytes);
    }

    @Override
    public String geometryToData(Geometry geometry) throws UnsupportedEncodingException {
        WKBWriter writer = new WKBWriter();
        byte[] write = writer.write(geometry);

        StringBuilder sb = new StringBuilder();
        for (byte b : write) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}
